package com.my.socket.aio.v3;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.Future;

/**
 * User: Alex Zhong
 * Date: 2016/6/17
 */
public class Client {
    private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);

    public static void main(String[] args) throws Exception {
        AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open();
        Future<Void> future = socketChannel.connect(new InetSocketAddress("127.0.0.1", 9999));
        future.get();

        String message = "I am Alex Zhong";
        socketChannel.write(ByteBuffer.wrap(message.getBytes())).get();
        LOGGER.info("send the message [{}] to the server", message);

        ByteBuffer byteBuffer = ByteBuffer.allocate(15);
        socketChannel.read(byteBuffer).get();
        message = new String(byteBuffer.array());
        LOGGER.info("receive the message [{}] from the server", message);
    }
}
